package com.lipiao.nb_iot;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class WarnFragment extends Fragment {
    View rootView;
    CardView person;
    CardView police;
    CardView fire;
    MediaPlayer mMediaPlayer;
    int i=0;
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_warn, container, false);
        person = rootView.findViewById(R.id.person);
        police = rootView.findViewById(R.id.police);
        fire = rootView.findViewById(R.id.fire);
        fire.setOnClickListener(new CalOnClick());
        initListener();
        return rootView;
    }

    class CalOnClick implements  View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.fire:
                    if (i % 2 == 0) {
                        mMediaPlayer = MediaPlayer.create(getActivity(), R.raw.fire);
                        mMediaPlayer.start();
                        Toast.makeText(getActivity(), "发出警报声！~", Toast.LENGTH_SHORT).show();
                        i++;
                        break;
                    }
                    if (i % 2 == 1) {
                        mMediaPlayer.stop();
                        Toast.makeText(getActivity(), "关闭警报声！~", Toast.LENGTH_SHORT).show();
                        i++;
                        break;
                    }
            }
        }
    }

//    @Override
//    public void onClick(View v) {
//        if(v.getId()==R.id.fire){
//            if(i%2==0){
//                mMediaPlayer=MediaPlayer.create(getActivity(), R.raw.fire);
//                mMediaPlayer.start();
//                Toast.makeText(getActivity(),"发出警报声！~",Toast.LENGTH_SHORT).show();
//                i++;
//            }
//            if(i%2==1){
//                mMediaPlayer.stop();
//                Toast.makeText(getActivity(),"关闭警报声！~",Toast.LENGTH_SHORT).show();
//                i++;
//            }
//        }
//    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //重载，进行自定义：获取statusFragment中的信息
        if (getUserVisibleHint()==true) {
            Log.d("碎片2", "从fragment1中获取的值" + StatusFragment.getTemperature());
        }

    }

    private void call() {
        /**
         * 检查App是否有permission.CALL_PHONE权限，就申请权限
         */
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

            /**
             * 没有permission.CALL_PHONE权限，就申请权限
             */
            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{Manifest.permission.CALL_PHONE}, 66);
        } else {
            callPhone();
        }
    }

    @SuppressLint("MissingPermission")
    private void callPhone() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri uri = Uri.parse("tel:" + "17700210443");
        intent.setData(uri);
        startActivity(intent);
    }

    /**
     * 申请权限的回调结果
     *
     * @param requestCode  请求码
     * @param permissions  请求权限
     * @param grantResults 授权结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 66) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                callPhone();
            } else {
                Toast.makeText(getActivity(),"权限被拒绝",Toast.LENGTH_LONG).show();
            }
        }
    }

    private void initListener() {
        person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity().getApplicationContext(), Person_Activity.class);
                startActivity(intent);
                //startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(getActivity(), cvLivingRoom, "image").toBundle());
            }
        });
        police.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                call();
                Toast.makeText(getActivity(),"报警成功！~",Toast.LENGTH_SHORT).show();
            }
        });
//        fire.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //发出警报声
//                mMediaPlayer=MediaPlayer.create(getActivity(), R.raw.fire);
//                mMediaPlayer.start();
//                Toast.makeText(getActivity(),"发出警报声！~",Toast.LENGTH_SHORT).show();
//                if(flag[0] ==0){
//                    mMediaPlayer.stop();
//                }
//                if(flag[0] ==1){
//                    flag[0] =0;
//                }
//            }
//        });
    }

}
